package HLLib.base;

import java.io.UnsupportedEncodingException;

import HLCode.HLClass;
import HLCode.HLClassManager;
import HLCode.HLClassManager_H;
import HLCode.HLLibObject;

public class HLString extends HLLibObject implements HLTime_H {
    public HLClass GetClass(HLClassManager classManager) {
        return classManager.GetLibClass(HLClassManager_H.CLASS_GROUP_TYPE_LIB_BASE, HLClassManager_H.LIB_CLASS_BASE_String);
    }

    public String string = "";

    public HLString() {
    }

    public HLString(String str) {
        string = str;
    }

    public void SetString(String str) {
        string = str;
    }

    public char CharAt(int index) {
        return string.charAt(index);
    }

    public int CompareTo(HLString anotherString) {
        return string.compareTo(anotherString.string);
    }

    public HLString Concat(HLString str) {
        return new HLString(string.concat(str.string));
    }

    public HLString ConcatInt(int i) {
        return new HLString(string + String.valueOf(i));
    }

    public HLString ConcatString(String str) {
        return new HLString(string.concat(str));
    }

    public boolean EndsWith(HLString suffix) {
        return string.endsWith(suffix.string);
    }

    public boolean Equals(HLString anotherString) {
        return string.equals(anotherString.string);
    }

    public boolean Equals1(String anotherString) {
        return string.equals(anotherString);
    }

    public HLByteList GetBytes() {
        return GetBytes(this);
    }

    public static HLByteList GetBytes(HLString string) {
        return new HLByteList(GetBytes1(string.string));
    }

    public static byte[] GetBytes1(String string) {
        try {
            return string.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
        }
        return string.getBytes();
    }

    public int HashCode() {
        return string.hashCode();
    }

    public int IndexOfChar(int ch) {
        return string.indexOf(ch);
    }

    public int IndexOfCharEX(int ch, int fromIndex) {
        return string.indexOf(ch, fromIndex);
    }

    public int IndexOfString(HLString str) {
        return string.indexOf(str.string);
    }

    public int IndexOfStringEX(HLString str, int fromIndex) {
        return string.indexOf(str.string, fromIndex);
    }

    public int LastIndexOfChar(int ch) {
        return string.lastIndexOf(ch);
    }

    public int LastIndexOfCharEX(int ch, int fromIndex) {
        return string.lastIndexOf(ch, fromIndex);
    }

    public int Length() {
        if (string == null) {
            string = "";
        }
        return string.length();
    }

    public HLString Replace(char oldChar, char newChar) {
        return new HLString(string.replace(oldChar, newChar));
    }

    public HLString ReplaceString(HLString oldString, HLString newstring) {
        HLString newString = new HLString();
        newString.string = replace(string, oldString.string, newstring.string);
        return newString;
    }

    static String replace(String strSource, String strFrom, String strTo) {
        if (strSource == null) {
            return null;
        }
        int i = 0;
        if ((i = strSource.indexOf(strFrom, i)) >= 0) {
            char[] cSrc = strSource.toCharArray();
            char[] cTo = strTo.toCharArray();
            int len = strFrom.length();
            StringBuffer buf = new StringBuffer(cSrc.length);
            buf.append(cSrc, 0, i).append(cTo);
            i += len;
            int j = i;
            while ((i = strSource.indexOf(strFrom, i)) > 0) {
                buf.append(cSrc, j, i - j).append(cTo);
                i += len;
                j = i;
            }
            buf.append(cSrc, j, cSrc.length - j);
            return buf.toString();
        }
        return strSource;
    }

    public HLList Split(char ch) {
        HLList strings = new HLList();
        if (string.length() == 0) {
            strings.Add(new HLString());
        } else {
            int start = 0;
            for (int p = 0; p < string.length(); start = p + 1) {
                p = string.indexOf(ch, start);
                if (p == -1) {
                    p = string.length();
                }
                strings.Add(new HLString(string.substring(start, p)));
            }
        }
        return strings;
    }

    public boolean StartsWith(HLString prefix) {
        return string.startsWith(prefix.string);
    }

    public HLString Substring(int beginIndex) {
        return new HLString(string.substring(beginIndex));
    }

    public HLString SubstringEX(int beginIndex, int endIndex) {
        return new HLString(string.substring(beginIndex, endIndex));
    }

    public String toString() {
        return string;
    }

    public HLString PadLeft(int length, char ch) {
        if (length <= string.length()) {
            return this;
        }

        StringBuffer sb = new StringBuffer();

        for (int i = length - string.length(); i > 0; i--) {
            sb.append(ch);
        }

        sb.append(string);

        return new HLString(sb.toString());
    }

    public HLString PadRight(int length, char ch) {
        if (length <= string.length()) {
            return this;
        }

        StringBuffer sb = new StringBuffer();

        sb.append(string);

        for (int i = length - string.length(); i > 0; i--) {
            sb.append(ch);
        }

        return new HLString(sb.toString());
    }

    public HLCharList ToCharArray() {
        return new HLCharList(string.toCharArray());
    }

    public HLString ToLowerCase() {
        return new HLString(string.toLowerCase());
    }

    public HLString ToUpperCase() {
        return new HLString(string.toUpperCase());
    }

    public HLString Trim() {
        return new HLString(string.trim());
    }

    public int ToInt() {
        return Integer.parseInt(string);
    }

    public int ToIntWithBase(int base) {
        return Integer.parseInt(string, base);
    }

    public boolean ToBoolean() {
        return string.toLowerCase().equals("true");
    }

    public static HLString ValueOfBoolean(boolean b) {
        return new HLString(String.valueOf(b));
    }

    public static HLString ValueOfBytes(byte[] data) {
        StringBuffer sb = new StringBuffer();
        int i = 0;
        while (i < data.length) {
            int c = data[i++] & 0xff;
            if (c < 128) {
                sb.append((char) c);
            } else if (c > 191 && c < 224) {
                sb.append((char) (((c & 0x1f) << 6) | (data[i++] & 0x3f)));
            } else {
                sb.append((char) (((c & 0xf) << 12) | ((data[i++] & 0x3f) << 6) | (data[i++] & 0x3f)));
            }
        }
        return new HLString(sb.toString());
    }

    public static HLString ValueOfByteList(HLByteList data) {
        return ValueOfBytes(data.items);
    }

    public static HLString ValueOfChar(char c) {
        return new HLString(String.valueOf(c));
    }

    public static HLString ValueOfCharList(HLCharList data) {
        return new HLString(String.valueOf(data.items));
    }

    public static HLString ValueOfSubCharList(HLCharList data, int offset, int count) {
        return new HLString(String.valueOf(data.items, offset, count));
    }

    public static HLString ValueOfInt(int i) {
        return new HLString(String.valueOf(i));
    }

    public static HLString ValueOfTime(int time, int startField, int endField, int endFieldCharCountMin) {
        if (endField > startField) {
            StringBuffer tempString = new StringBuffer();

            for (int i = startField; i < endField; i++) {
                tempString.insert(0, ':');
                switch (i) {
                case CENTISECOND: {
                    tempString.insert(1, ValueOfInt(time % 100).PadLeft(2, '0').string);
                    time /= 100;
                    break;
                }
                case SECOND:
                case MINUTE: {
                    tempString.insert(1, ValueOfInt(time % 60).PadLeft(2, '0').string);
                    time /= 60;
                    break;
                }
                }
            }

            return new HLString(ValueOfInt(time).PadLeft(endFieldCharCountMin, '0').string + tempString.toString());
        } else {
            return ValueOfInt(time).PadLeft(endFieldCharCountMin, '0');
        }
    }

    public static final boolean IsNum(char ch) {
        return (ch >= '0' && ch <= '9');
    }

    public static final boolean IsChar(char ch) {
        return (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z');
    }

    static public HLString GetLanguageTextByScript(HLDictionary languageTextDictionary, HLString script) {
        if (script.StartsWith(new HLString("$LANGUAGE$"))) {
            HLString tempString = script.Substring("$LANGUAGE$".length());

            if (languageTextDictionary.ContainsKey(tempString)) {
                return (HLString) languageTextDictionary.GetItem(tempString);
            }
        }

        return script;
    }

    public static HLString UrlEncode(HLString string) {
        return new HLString(string.string);
    }

    public static HLString UrlDecode(HLString string) {
        return new HLString(string.string);
    }
    
    public static void main(String[] args) {
        System.out.println("321".concat("123"));
    }
}
